package com.yuanqunsoft.alimigration.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.hibernate.*;

import com.yuanqunsoft.alimigration.dao.IRuleSetDao;
import com.yuanqunsoft.alimigration.model.AllUser;
import com.yuanqunsoft.alimigration.model.Remediation;
import com.yuanqunsoft.alimigration.model.ReportWithRuleset;
import com.yuanqunsoft.alimigration.model.Rule;
import com.yuanqunsoft.alimigration.model.RuleSet;
import com.yuanqunsoft.alimigration.model.RulesetWithRule;

public class RuleSetDaoImpl extends HibernateDaoSupport implements IRuleSetDao {
  
    private int currentPage;
    private int pageSize;
    private int allPage;
    @Override
    public List<Rule> getRuleByRuleSetId(int id) {
        // TODO Auto-generated method stub
        String hql = "from RulesetWithRule rs where rs.ruleSet.rusId=?";
        List<RulesetWithRule> rs = this.getHibernateTemplate().find(hql, id);
        List<Rule> rules = new ArrayList<Rule>();
        for(int i = 0; i < rs.size(); i++){
            rules.add(rs.get(i).getRule());
            
        }
        return rules;
    }

    @Override
    public boolean save(RuleSet rs, List<Integer> ruleIds) {
        // TODO Auto-generated method stub
        try{
            this.getHibernateTemplate().save(rs);
            this.getHibernateTemplate().flush();
            RulesetWithRule rws;
            Rule r;
            if(ruleIds != null){
                for(int i = 0; i < ruleIds.size(); i++){
                    rws = new RulesetWithRule();
                    r = (Rule)this.getHibernateTemplate().get(Rule.class, ruleIds.get(i).intValue());
                    rws.setRule(r);
                    rws.setRuleSet(rs);
                    this.getHibernateTemplate().save(rws);
                    this.getHibernateTemplate().flush();
                }
            }
        }catch(Exception e){
            return false;
        }
        return true;
    }

    public boolean delete(RuleSet ruleSet) {
        // TODO Auto-generated method stub
        try{
            String hql = "from RulesetWithRule r where r.ruleSet.rusId=?";
            List<RulesetWithRule> rsw = (List<RulesetWithRule>)this.getHibernateTemplate().find(hql, ruleSet.getRusId());
            for(int i = 0; i < rsw.size(); i++) {
                this.getHibernateTemplate().delete(rsw.get(i));
            }
            String hqlc = "from ReportWithRuleset r where r.ruleSet.rusId=?";
            List<ReportWithRuleset> rwr = this.getHibernateTemplate().find(hqlc, ruleSet.getRusId());
            for(int i = 0; i < rwr.size(); i++){
                this.getHibernateTemplate().delete(rwr.get(i));
            }
            this.getHibernateTemplate().delete(ruleSet);
        }catch(Exception e){
            return false;
        }
        return true;
    }


    @Override
    public boolean update(RuleSet rs) {
        // TODO Auto-generated method stub
        try{
            this.getHibernateTemplate().update(rs);
        }catch(Exception e){
            return false;
        }
        return true;
    }

    @Override
    public List<RuleSet> getAllRuleSet() {
        // TODO Auto-generated method stub
        String hql = "from RuleSet";
        return this.getHibernateTemplate().find(hql);
    }

    @Override
    public List<RuleSet> queryPage(final int currentPage, final int pageSize) {
        // TODO Auto-generated method stub
        return (List<RuleSet>)this.getHibernateTemplate().execute(new HibernateCallback() {
            
            public Object doInHibernate(Session s) {
            
                String hql = "from RuleSet";
                Query q = s.createQuery(hql);
                List<RuleSet> r1 = q.list();
                allPage = (r1.size() + pageSize - 1) / pageSize;
                q = s.createQuery(hql);
                q.setMaxResults(pageSize);
                q.setFirstResult((currentPage - 1) * pageSize);
                return q.list();
            }
        });
    }

    @Override
    public RuleSet getRuleSetById(int id) {
        // TODO Auto-generated method stub
        return this.getHibernateTemplate().get(RuleSet.class, id);
    }

    @Override
    public void selectRules(RuleSet rs, List<Integer> ruleIds) {
        // TODO Auto-generated method stub
        RulesetWithRule rw;
        String hql = "from RulesetWithRule r where r.ruleSet.rusId=?";
        List<RulesetWithRule> rws  = this.getHibernateTemplate().find(hql, rs.getRusId());
        for(int i = 0; i < rws.size(); i++){
            this.getHibernateTemplate().delete(rws.get(i));
        }
        Rule r;
        for(int i = 0; i < ruleIds.size(); i++){
          r = (Rule)this.getHibernateTemplate().get(Rule.class, ruleIds.get(i).intValue());
          rw = new RulesetWithRule();
          rw.setRule(r);
          rw.setRuleSet(rs);
          this.getHibernateTemplate().save(rw);
        }
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getAllPage() {
        return allPage;
    }

    public void setAllPage(int allPage) {
        this.allPage = allPage;
    }

    @Override
    public List<RuleSet> findByRuleId(int id) {
        String hql = "from RulesetWithRule rs where rs.rule.rulId=?";
        List<RulesetWithRule> rs = this.getHibernateTemplate().find(hql, id);
        List<RuleSet> rulesets = new ArrayList<RuleSet>();
        for(int i = 0; i < rs.size(); i++){
            rulesets.add(rs.get(i).getRuleSet());
        }
        return rulesets;
    }

    @Override
    public AllUser getUserById(int id) {
        // TODO Auto-generated method stub
        return (AllUser)this.getHibernateTemplate().get(AllUser.class, id);
    }

    
}
